import { LocalStorageUtils } from '@/utils/tyutils'

const state = {
    userinfo: {
        username: undefined,
        token: undefined,
        section: undefined
    }
}
const mutations = {
    setUserInfo(state: any, userinfo: any) {
        try{
            let obj = {
                username: undefined,
                token: undefined,
                section: undefined
            }
            let options:any = {}
            Object.assign(options,obj,userinfo)
            state.userinfo.username = options.username
            state.userinfo.token = options.token
            state.userinfo.section = options.section
            LocalStorageUtils.setItem({
                name: "userinfo",
                value: options,
                expires: 720
            })
        }catch (err){
            console.log(err.toString())
        }
    },
    logout(state: any) {
        state.userinfo.username = undefined
        state.userinfo.token = undefined
        state.userinfo.section = undefined
        LocalStorageUtils.removeItem("userinfo")
    }
}
const actions = {
    setUserInfo(context: any, userinfo: any){
        context.commit("setUserInfo",userinfo)
    },
    logout(context: any){
        context.commit("logout")
    }
}
const getters = {
    /**
     * 查找用户信息
     * 若无用户信息,则为未登录,返回false
     * @param state
     */
    getUserInfo:(state: any)=>{
        let result = state.userinfo
        if(typeof result.token == 'undefined') result = LocalStorageUtils.getItem("userinfo")
        if(!result) return false
        state.userinfo = result
        return result
    },
    /**
     * 查找用户拥有权限的标段
     * @param state
     */
    getUserSection:(state: any)=>{
        let result = state.userinfo
        if(typeof result.token == 'undefined') result = LocalStorageUtils.getItem("userinfo")
        if(!result) return false
        state.userinfo = result
        return result.section
    },
    /**
     * 判断是否拥有指定标段,指定功能的权限
     * @param state
     */
    isPermission:(state: any)=>{
        /**
         * @param sectionId 标段id
         * @param kindType 模块类型标识
         * @param permissionType 权限类型标识 0:编辑,1:查看
         */
        return (sectionId: number, kindType: number, permissionType: number):boolean=>{
            // 先判断有无用户信息
            let userinfo = state.userinfo
            if(typeof userinfo.token == 'undefined') userinfo = LocalStorageUtils.getItem("userinfo")
            if(!userinfo) return false
            state.userinfo = userinfo

            // 查询是否有某标段权限
            let sectioninfo = userinfo.section.find((item: any)=>{
                return item.sectionId == sectionId
            })
            // 查询是否有某功能权限
            if(sectioninfo){
                let craftinfo = sectioninfo.craft.find((item: any)=>{
                    return item.kindType == kindType && item.permissionType == permissionType
                })
                return craftinfo ? true : false
            }else{
                return false
            }
        }
    }
}

export default {
    namespaced:true,
    state: state,
    mutations: mutations,
    actions: actions,
    getters: getters
}
